/**
 * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.hellodoctor.slayer.service.impl;

import java.util.Calendar;
import java.util.List;

import com.hellodoctor.slayer.NoSuchProfileException;
import com.hellodoctor.slayer.model.Profile;
import com.hellodoctor.slayer.model.impl.ProfileImpl;
import com.hellodoctor.slayer.service.base.ProfileLocalServiceBaseImpl;
import com.liferay.portal.kernel.dao.orm.DynamicQuery;
import com.liferay.portal.kernel.dao.orm.DynamicQueryFactoryUtil;
import com.liferay.portal.kernel.dao.orm.RestrictionsFactoryUtil;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.util.Validator;


/**
 * The implementation of the profile local service.
 *
 * <p>
 * All custom service methods should be put in this class. Whenever methods are added, rerun ServiceBuilder to copy their definitions into the {@link com.hellodoctor.slayer.service.ProfileLocalService} interface.
 *
 * <p>
 * This is a local service. Methods of this service will not have security checks based on the propagated JAAS credentials because this service can only be accessed from within the same VM.
 * </p>
 *
 * @author Shahbaj
 * @see com.hellodoctor.slayer.service.base.ProfileLocalServiceBaseImpl
 * @see com.hellodoctor.slayer.service.ProfileLocalServiceUtil
 */
public class ProfileLocalServiceImpl extends ProfileLocalServiceBaseImpl {
	/*
	 * NOTE FOR DEVELOPERS:
	 *
	 * Never reference this interface directly. Always use {@link com.hellodoctor.slayer.service.ProfileLocalServiceUtil} to access the profile local service.
	 */
	
	public Profile createProfile (long userId, boolean isDoctor){
		long profileId =0l;
	try {
		profileId = counterLocalService.increment(Profile.class.getName());
	} catch (SystemException e) {
		e.printStackTrace();
	}
	Profile profile = new ProfileImpl();
	profile.setProfileId(profileId);
	profile.setUserId(userId);
	profile.setIsDoctor(isDoctor);
	profile.setCreateDate(new java.util.Date());
	try {
		profile=profileLocalService.addProfile(profile);
	} catch (SystemException e) {
		e.printStackTrace();
	}
	return profile;
	}
	
/*	@Override
	public Profile updateProfile(Profile profile) throws SystemException {

		profile.setModifiedDate(new java.util.Date());
		return super.updateProfile(profile);
	}*/
	
	public Profile getProfileByUserId(long userId) {
		Profile profile = new ProfileImpl();
		try {
			profile = profilePersistence.findByUserId(userId);
		} catch (NoSuchProfileException e) {
			e.printStackTrace();
		} catch (SystemException e) {
			e.printStackTrace();
		}
		return profile;
	}
	
	public List<Profile> searchDoctors(int speciality, int qualification , int experience)
			throws SystemException {
		int currYear = Calendar.getInstance().get(Calendar.YEAR);
		
		DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(Profile.class);
		dynamicQuery.add(RestrictionsFactoryUtil.eq("isDoctor", true));
		if (!Validator.equals(-1, speciality))
			dynamicQuery.add(RestrictionsFactoryUtil.eq("speciality",speciality));
		if (!Validator.equals(-1, qualification))
			dynamicQuery.add(RestrictionsFactoryUtil.eq("qualification",qualification));
		if(!Validator.equals(-1, experience) && !Validator.equals(5, experience))
			dynamicQuery.add(RestrictionsFactoryUtil.between("practicingSince", currYear-(experience *5), currYear-(experience+1 *5)));	
		if( Validator.equals(5, experience))
			dynamicQuery.add(RestrictionsFactoryUtil.ge("practicingSince", currYear-(experience *5)));	
		return dynamicQuery(dynamicQuery);
	}
}